<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%>
<%@page import="com.liferay.portal.kernel.util.JavaConstants"%>
<%@page import="javax.portlet.PortletRequest"%>
<%@page import="com.bkav.portal.portlet.product.search.ProductSearch"%>
<%@page import="org.apache.commons.beanutils.BeanComparator"%>
<%@page
	import="com.bkav.portal.portlet.permission.productCategoryPermission"%>
<%@page import="com.bkav.portal.portlet.permission.productPermission"%>
<%@page import="com.liferay.portal.kernel.util.HtmlUtil"%>
<%@page import="com.liferay.portal.kernel.dao.search.SearchContainer"%>
<%@page import="com.liferay.portal.kernel.util.ListUtil"%>

<%@page
	import="com.bkav.portal.portlet.product.service.ProductCategoryLocalServiceUtil"%>
<%@page import="com.bkav.portal.portlet.product.model.ProductCategory"%>
<%@page import="com.bkav.portal.portlet.product.model.Product"%>
<%@page
	import="com.bkav.portal.portlet.product.service.ProductLocalServiceUtil"%>
<%@page import="com.liferay.portal.kernel.dao.orm.DynamicQuery"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@include file="/html/product_admin/init.jsp"%>
<%
    boolean closeDialog = false;

    PortletRequest portletRequest = (PortletRequest)request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);

    if(SessionErrors.isEmpty(portletRequest) && !SessionMessages.isEmpty(portletRequest)){
	    closeDialog = true;
    }
    String productAdminPortletResource = "product_admin_WAR_productportlet";
	String productCommand = ParamUtil.getString(request,
	"productCommand");
	long productId = ParamUtil.getLong(request, "productId", 0L);
	long categoryId = ParamUtil.getLong(request, "category", 0L);
	String productSearchKeyword = ParamUtil.getString(request, "productSearchKeyword");
	int productIndex = 0;
	Product product = null;
	if (productId > 0L) {
		product = ProductLocalServiceUtil.getProduct(productId);
	}
	ProductCategory productCategory = null;
	if (categoryId > 0L) {
		productCategory = ProductCategoryLocalServiceUtil
		.getProductCategory(categoryId);
	}
	List<Product> productList = ProductLocalServiceUtil.findByKeyword(groupId, productSearchKeyword, 0, 10);
	List<ProductCategory> productCategoryList = ProductCategoryLocalServiceUtil.findByGroup(groupId);
%>

<!-- .product -->
<div class="product">

	<liferay-portlet:renderURL var="productAdminHomePageURL">
		<liferay-portlet:param name="jspPage"
			value="/html/product_admin/view.jsp" />
		<liferay-portlet:param name="tabs1" value="product" />
	</liferay-portlet:renderURL>

	<liferay-portlet:renderURL var="productInsertFormURL">
		<liferay-portlet:param name="jspPage"
			value="/html/product_admin/view.jsp" />
		<liferay-portlet:param name="tabs1" value="product" />
		<liferay-portlet:param name="productCommand" value="updateProduct" />
	</liferay-portlet:renderURL>

	<div class="lfr-header-row">
		<div class="lfr-header-row-content">
			<!-- .add_new_product -->
			<div class="add_new_product_left" align="left">
				<c:if
					test="<%=productPermission.contains(permissionChecker, groupId, productPermission.ADD)%>">
					<aui:button type="button" value="Thêm sản phẩm"
						href="<%=productInsertFormURL%>" name="productButton" />
				</c:if>
				<c:if
					test="<%=productPermission.contains(permissionChecker, groupId, productPermission.PERMISSIONS)%>">
					<liferay-security:permissionsURL
						modelResource="<%=Product.class.getName()%>"
						modelResourceDescription="<%=HtmlUtil.escape(themeDisplay.getScopeGroupName())%>"
						resourcePrimKey="<%=String.valueOf(groupId)%>"
						var="permissionsURL" />
					<aui:button name="productPermissionsBtn" href="<%=permissionsURL%>"
						value="permissions" />
				</c:if>
			</div>

			<!-- .product_category_search_form -->
			<div class="product_search_form" align="right">
				<aui:form name="product_search_form" method="post"
					action="<%=productAdminHomePageURL%>">
					<input type="text" id="<portlet:namespace/>categorySearchInput"
						name="productSearchKeyword" value='<%=productSearchKeyword%>' />
					<aui:button type="submit" value="Tìm kiếm" />
					<aui:button type="button" value="Hiển thị toàn bộ"
						href="<%=productAdminHomePageURL%>" />
				</aui:form>
			</div>
		</div>
	</div>

	<c:if test='<%=Validator.equals(productCommand, "updateProduct")%>'>
		<!-- .product-update-form -->
		<div class="productCategory-update-form" align="center">
			<liferay-portlet:actionURL name="updateProduct"
				var="updateProductActionURL">
				<liferay-portlet:param name="productId"
					value="<%=String.valueOf(productId)%>" />
			</liferay-portlet:actionURL>
			<aui:form name="product_update_form" method="post"
				action="<%=updateProductActionURL%>">
				<table>
					<tr>
						<td>Tên sản phẩm</td>
						<td><aui:input type="text" label="" name="productName"
								value='<%=(product != null) ? product.getProductName()
								: ""%>'></aui:input>
						</td>
					</tr>

					<tr>
						<td>Chuyên mục</td>
						<td><aui:select name="categoryId" label="">
								<aui:option></aui:option>
								<%
									for (ProductCategory productCategoryItem : productCategoryList) {
								%>
								<aui:option value="<%=productCategoryItem.getCategoryId()%>"><%=(productCategory != null) ? productCategory
										.getCategoryName()
										: productCategoryItem.getCategoryName()%></aui:option>
								<%
									}
								%>
							</aui:select></td>
					</tr>
					<tr>
						<td>Thông tin sản phẩm</td>
						<td><aui:input type="textarea" label="" name="productInfo"
								value='<%=(product != null) ? product.getProductInfo()
								: ""%>'></aui:input>
						</td>
					</tr>
					<tr>
						<td></td>
						<c:if
							test="<%=productPermission.contains(permissionChecker, groupId, productPermission.UPDATE)%>">
							<td><aui:button name="save" value="Save" type="submit"></aui:button>
								<aui:button name="cancle" value="Cancle" type="button"></aui:button>
							</td>
						</c:if>
					</tr>
				</table>
			</aui:form>


		</div>
	</c:if>
   <%
	PortletURL portletURL = renderResponse.createRenderURL();
    

	ProductSearch productSearch = new ProductSearch(renderRequest,portletURL);
   %>
	<div class="product">
		<liferay-ui:search-container searchContainer="<%=productSearch%>">
		    <%
		     List<Product> listProduct = ProductLocalServiceUtil.getProducts(searchContainer.getStart(), searchContainer.getEnd(), searchContainer.getOrderByComparator()); 
		    //logSearch.getOrderByComparator()
	     	%>
			<liferay-ui:search-container-results
				results="<%=listProduct %>"
				total="<%=listProduct.size() %>"
			/>			
				
			<liferay-ui:search-container-row className="Product" keyProperty="productId" modelVar="product_1" >
				<%
					productIndex++;
					
					int productCounter = ProductLocalServiceUtil.countAll();
					String updateVideoURL = "javascrip:;";
	
		
                    updateVideoURL = "javascript:" + renderResponse.getNamespace() 
							+ "showOrderForm(" + String.valueOf(product_1.getProductId()) + ");";
				%>
				
				<liferay-portlet:renderURL var="updateProductURL" >				
					<liferay-portlet:param name="jspPage" value="/html/product_admin/view.jsp"/>
					<liferay-portlet:param name="tabs1" value="product"/>
					<liferay-portlet:param name="productCommand" value="updateProduct"/>
					<liferay-portlet:param name="productId" value="<%=String.valueOf(product_1.getProductId())%>"/>
				</liferay-portlet:renderURL>
								
				<liferay-ui:search-container-column-text name="#" value="<%=String.valueOf(productIndex) %>" />
					
				<liferay-ui:search-container-column-text name="Tên sản phẩm" value="<%=product_1.getProductName()%>" orderable="<%=true %>"
				orderableProperty="productName"/>
				
				<liferay-ui:search-container-column-text name="Ngày tạo" value="<%=String.valueOf(product_1.getCreateDate())%>" orderable="<%=true %>"
				orderableProperty="createDate"/>
				
				<liferay-ui:search-container-column-text name="Thông tin sản phẩm" value="<%=product_1.getProductInfo()%>"/>
				
				<liferay-ui:search-container-column-text name="edit">
					<c:if test="<%=productPermission.contains(permissionChecker, groupId, productPermission.UPDATE)%>">														
						<liferay-ui:icon image="edit" message="edit" url="<%=updateProductURL %>" />
					</c:if>
				</liferay-ui:search-container-column-text>
								
				<liferay-ui:search-container-column-text name="delete">
					<c:if test="<%=productPermission.contains(permissionChecker, groupId, productPermission.DELETE)%>">															
						<liferay-portlet:actionURL  name="deleteProduct" var="deleteProductURL">
							<liferay-portlet:param name="productId" value="<%=String.valueOf(product_1.getProductId())%>"/>
							<liferay-portlet:param name="redirectURL" value="<%=productAdminHomePageURL%>"/>
						</liferay-portlet:actionURL>
						
						<liferay-ui:icon-delete url="<%= deleteProductURL %>" />
					</c:if>
				</liferay-ui:search-container-column-text>
				
				<liferay-ui:search-container-column-text name="order" value="order" href="<%=updateVideoURL %>"/>
			</liferay-ui:search-container-row>
			
			<liferay-ui:search-iterator paginate="<%=false %>"/>
		</liferay-ui:search-container>
		
	</div>
	<script type="text/javascript">
		AUI()
				.ready(
						'aui-base,aui-form-validator,aui-overlay-context-panel',
						function(A) {

							var categorySearchInput = A
									.one('#<portlet:namespace/>categorySearchInput');

							if (categorySearchInput) {
								categorySearchInput.focus();
							}

							if (<%=Validator.equals(productCommand, "updateProduct")%>) {

								var productNameInput = A
										.one('#<portlet:namespace />productName');

								if (productNameInput) {
									productNameInput.focus();
								}

								window.validator = new A.FormValidator(
										{

											boundingBox : document.<portlet:namespace />product_update_form,

											rules : {
												<portlet:namespace />productName : {
													required : true,
													rangeLength : [ '3', '300' ]
												},
												<portlet:namespace />productInfo : {
													required : false,
													rangeLength : [ '0', '450' ]
												}
											}
										});
							}
						});
	</script>
	<aui:script>

	AUI().ready('aui-base','liferay-portlet-url',function(A){
	if(<%=closeDialog %>){
			
			Liferay.fire(
				'closeWindow',
				{
					id: '<portlet:namespace/>weblinkAdminDialog',
				},window.top.location.reload(true)
			);
		}
		
	});
							
	Liferay.provide(
		window,
		'<portlet:namespace/>showOrderForm',
		function(productId){
			
			var updateVideoURL = Liferay.PortletURL.createRenderURL();
										
			updateVideoURL.setPortletId('<%=productAdminPortletResource %>');
			updateVideoURL.setWindowState('pop_up');
			updateVideoURL.setParameter('jspPage', '/html/product_admin/product/order.jsp');
			updateVideoURL.setParameter('productId', productId);
			updateVideoURL.setDoAsGroupId('<%=groupId %>');
						
			var dialogWidow = {
				dialog: {
					align: Liferay.Util.Window.ALIGN_CENTER,
					modal: true,
					width: 400,
					cache: false
				},
				title: Liferay.Language.get('order'),
				uri: updateVideoURL
			};
			
			Liferay.Util.openWindow(dialogWidow);
		}
	);
				
	Liferay.provide(
		window,
		'refresh',
		function(){
			Liferay.Portlet.refresh('#p_p_id=product_admin_WAR_productportlet_');
		}
	);
</aui:script>